<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<form [formGroup]="form" *ngIf="form" (ngSubmit)="onCreate()">
  <h2 mat-dialog-title class="title">Create a subscription</h2>

  <mat-dialog-content class="subscription-creation__content">
    <ng-container *ngIf="plans?.length > 0; else noPlansForCreation">
      <label id="application-label">Select an application</label>
      <mat-form-field class="subscription-creation__content__applications" aria-labelledby="application-label">
        <mat-icon matPrefix>search</mat-icon>
        <mat-label>Search an application by name</mat-label>
        <!--Using focus workaround to avoid label overlapping border https://github.com/angular/components/issues/15027#issuecomment-1335147036-->
        <input matInput type="search" [matAutocomplete]="auto" formControlName="selectedApplication" />

        <mat-autocomplete #auto="matAutocomplete" [displayWith]="displayApplication">
          <mat-option
            *ngFor="let application of applications$ | async"
            [value]="application"
            class="subscription-creation__content__applications__option"
          >
            <div class="subscription-creation__content__applications__option__title">
              <gio-avatar [src]="application.picture_url" [name]="application.name" [size]="32" [roundedBorder]="true"></gio-avatar>
              {{ application.name }}
              <small>({{ application.owner?.displayName }})</small>
            </div>
          </mat-option>
          <mat-option *ngIf="(applications$ | async)?.length === 0" disabled>No application matching</mat-option>
        </mat-autocomplete>
        <mat-error *ngIf="form.get('selectedApplication').hasError('selectionRequired')"
          >Please select an application from the list</mat-error
        >
        <mat-error *ngIf="form.get('selectedApplication').hasError('clientIdRequired')"
          >JWT and OAuth2 plans require an application with a clientId</mat-error
        >
      </mat-form-field>

      <label id="radio-group-label">Select a plan to subscribe</label>
      <mat-radio-group
        aria-labelledby="radio-group-label"
        formControlName="selectedPlan"
        arial-label="Select an plan"
        class="subscription-creation__content__plans"
        [disabled]="!form.get('selectedApplication').value?.id"
      >
        <mat-radio-button
          *ngFor="let plan of plans"
          [value]="plan"
          [disabled]="!!plan.generalConditions || isPlanSubscribedBySelectedApp(plan)"
        >
          {{ plan.name }}
        </mat-radio-button>
      </mat-radio-group>

      <ng-container *ngIf="form.get('apiKeyMode')">
        <div class="mat-body-2">
          <p>You have to choose between two modes for your application :</p>
          <ul>
            <li><b>API Key</b> - a new API Key will be generated for each subscription</li>
            <li><b>Shared API Key</b> - each subscription will use the same API Key</li>
          </ul>
        </div>
        <div class="subscription-creation__content__keymode__warning">
          <p>Please note that this choice is permanent.</p>
        </div>
        <mat-radio-group
          aria-labelledby="radio-group-label"
          formControlName="apiKeyMode"
          arial-label="API Key Mode"
          class="subscription-creation__content__keymode"
        >
          <mat-radio-button value="EXCLUSIVE">API Key</mat-radio-button>
          <mat-radio-button value="SHARED">Shared API Key</mat-radio-button>
        </mat-radio-group>
      </ng-container>

      <ng-container *ngIf="form.get('customApiKey')">
        <div class="mat-body-2">
          You can provide a custom API Key if you already have one. <br />
          Leave it blank to get a generated API Key.
        </div>
        <api-key-validation
          formControlName="customApiKey"
          [applicationId]="form.get('selectedApplication').value.id"
          [apiId]="form.get('selectedPlan').value.apiId"
        ></api-key-validation>
      </ng-container>

      <ng-container *ngIf="form.get('selectedEntrypoint')">
        <mat-form-field>
          <mat-label>Select the entrypoint to subscribe</mat-label>
          <mat-select formControlName="selectedEntrypoint" placeholder="Select an entrypoint">
            <mat-option *ngFor="let entrypoint of availableSubscriptionEntrypoints" [value]="entrypoint.type">
              <mat-icon [svgIcon]="entrypoint.icon"></mat-icon> {{ entrypoint.name }}
            </mat-option>
          </mat-select>
        </mat-form-field>

        <mat-form-field class="subscription-creation__content__channel">
          <mat-label>Channel</mat-label>
          <input matInput type="text" formControlName="channel" />
          <mat-hint>You can provide a channel for the subscription</mat-hint>
        </mat-form-field>

        <gio-form-json-schema
          *ngIf="form.get('entrypointConfiguration') && selectedSchema"
          [jsonSchema]="selectedSchema"
          formControlName="entrypointConfiguration"
        ></gio-form-json-schema>
      </ng-container>
    </ng-container>
    <ng-template #noPlansForCreation>
      <div>You don't have any available plan. To subscribe, you need to have at least one published plan whose type is not Keyless.</div>
    </ng-template>
  </mat-dialog-content>

  <mat-dialog-actions class="actions">
    <button type="button" class="actions__cancelBtn" mat-flat-button [mat-dialog-close]="false">Cancel</button>
    <button
      type="submit"
      class="actions__createBtn"
      color="primary"
      mat-raised-button
      role="dialog"
      [disabled]="this.form.invalid || this.form.pristine"
    >
      Create
    </button>
  </mat-dialog-actions>
</form>
